<template>
    <d2-container>

        <el-form :inline="true" >

            <el-form-item label="登录账户" >
                <el-input v-model="dataSearch.username" placeholder="登录账户"></el-input>
            </el-form-item>


            <el-form-item label="关联员工" >
                <el-input v-model="dataSearch.staff" placeholder="关联员工"></el-input>
            </el-form-item>
            <el-form-item label="校区" v-if='headquarter'>
                            <el-select v-if='headquarter'
                                    v-model="dataSearch.school"
                                    filterable
                                    placeholder="请选择校区">
                                <el-option
                                        v-for="item in schoolList"
                                        :key="item._id"
                                        :label="item.name"
                                        :value="item._id"
                                ></el-option>
                            </el-select>
            </el-form-item>


<!--            <el-form-item label="邮箱" prop="email">-->
<!--                <el-input v-model="dataSearch.email" placeholder="请输入邮箱"></el-input>-->
<!--            </el-form-item>-->


            <el-form-item>
                <el-button type="primary" @click="formSubmit">
                    <d2-icon name="search"/>
                    查询
                </el-button>
            </el-form-item>

            <el-form-item>
                <el-button @click="formReset">
                    <d2-icon name="refresh"/>
                    重置
                </el-button>
            </el-form-item>

            <a href="#/user-add">
                <el-button type="primary" style="float:right" icon="el-icon-plus">新建用户</el-button>
            </a>

        </el-form>

        <el-dialog
                title="更改密码"
                :visible.sync="dialogVisibleStatus"
                width="30%"
        >

            <span>请输入需要更改的密码</span>

            <el-input v-model="password" placeholder="请输入密码"></el-input>


            <span slot="footer" class="dialog-footer">
             <el-button @click="dialogVisibleStatus = false">取 消</el-button>
             <el-button type="primary" @click="updatePassword">确 定</el-button>
          </span>
        </el-dialog>

        <el-table :data="dataList" border style="width: 100%">
            <el-table-column prop="username" label="登录账户"></el-table-column>
<!--            <el-table-column prop="cellphone" label="用户手机"></el-table-column>-->
            <!-- <el-table-column prop="gender" label="用户性别"></el-table-column> -->
<!--            <el-table-column prop="city" label="城市"></el-table-column>-->
<!--            <el-table-column prop="province" label="省份"></el-table-column>-->
<!--            <el-table-column prop="nickName" label="昵称"></el-table-column>-->
<!--            <el-table-column prop="name" label="真实姓名"></el-table-column>-->
<!--            <el-table-column prop="email" label="邮箱"></el-table-column>-->
<!--            <el-table-column prop="wechat" label="微信"></el-table-column>-->
            <el-table-column prop="school" label="校区"></el-table-column>
            <el-table-column prop="staff.name" label="关联员工"></el-table-column>

            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                    <!-- <el-button @click="changePassword(scope.row)" type="text" size="small">更改密码</el-button> -->
                    <el-button @click="changeUser(scope.row)" type="text" size="small">修改</el-button>
                                        <el-button @click="deleteClick(scope.row)" type="text" size="small" v-if='scope.row.username != "root"'>删除</el-button>

                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="total,sizes,prev, pager, next,jumper"
                :page-size="pageSize"
                :current-page="currentPage"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handlerSizeChange"
                @next-click="handleNextPage"
                @prev-click="handlePrevClick"
        ></el-pagination>
    </d2-container>
</template>

<script>
    import request from "@/plugin/axios";
    import {mapState} from "vuex";

    export default {
        data() {
            return {
                dataList: [],
                total: 0,
                pageSize: 10,
                currentPage: 1,
                classroom: [],
                dataSearch: {
                    username:undefined,
                    staff:undefined,
                    school : undefined
                },
                dialogVisibleStatus: false,
                password: undefined,
                temp_id: undefined,
                schoolList: [],

            };
        },
        computed:{
            ...mapState('d2admin/user', ['info']),
        },
        methods: {

            formSubmit() {
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },

            formReset() {
                this.$refs.dataSearch.resetFields();
            },

            handleClick(e) {
                this.$router.push({path: "/user-add?id=" + e._id});
            },

            handleCurrentChange(currentPage) {
                this.currentPage = currentPage;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },

            handlerSizeChange(pageSize) {
                this.pageSize = pageSize;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },

            handleNextPage() {
                this.currentPage = this.currentPage + 1;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },

            handlePrevClick() {
                this.currentPage = this.currentPage - 1;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },


            changeUser(e){
                this.$router.push({path:'/user-detail?id=' + e._id})
            },
            deleteClick(e) {

                this.$confirm("此操作将删除该用户, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        request({
                            url: "/user/deleteUser",
                            method: "post",
                            data: {_id: e._id,school:this.school}
                        }).then(res => {
                            if (res.code == 200) {
                                this.$message({
                                    message: "删除成功"
                                });
                                this.getDataList(this.dataSearch);
                            }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },

            changePassword(e) {
                this.dialogVisibleStatus = true;
                this.temp_id = e._id

            },
            updatePassword() {

                request({
                    url: "/user/changePasswordByRoot",
                    method: "post",
                    data: {_id: this.temp_id, password: this.password}
                }).then(res => {
                    console.log(res)
                    this.dialogVisibleStatus = false;
                    if (res.code == 200) {
                        this.$message({
                            message: "更改成功"
                        });
                        this.getDataList(this.dataSearch);
                    }
                });
            },
            getSchoolList() {
                request({
                    url: "/school/getSchoolList",
                    method: "post",
                    data: {page: 1, pageSize: 99999}
                }).then(res => {
                    this.schoolList = [{name:'全部',_id:''}]
                    this.schoolList = this.schoolList.concat(res.data.school)

                    // if (this.info.school[0].headquarter == 1){
                    //        this.schoolList = res.data.school
                    //     }
                    //     else{
                    //         const ak = this.school
                    //         this.schoolList = res.data.school.filter(function(e){return e._id == ak})
                    //     }
                    
                })
            },
            getDataList(data) {
                let isRoot = 2;
                this.info.role.forEach(value => {
                    if (value.name === 'root') {
                        isRoot = 1
                    }

                })
                if (this.info.school[0].headquarter == 1){
                    isRoot = 1
                }
                if (this.info.school[0].headquarter == 2){
                    isRoot = 1
                }
                // isRoot = 2
                if (isRoot === 1) {
                    let data1 = {};
                    data1.page = 1;
                    data1.pageSize = 9999999;
                    // request({
                    //     url: "/school/getSchoolList",
                    //     method: "post",
                    //     data1
                    // }).then(res => {
                    //     data.school = res.data.school.map(value => value._id);
                    //     this.schoolList = res.data.school
                    if (this.dataSearch.school._id == ''){
                        data.school = this.schoolList.map(value => value._id)
                    }

                        request({
                            url: "/user/getUserList",
                            method: "post",
                            data: data
                        }).then(res => {
                            this.dataList = [];
                            this.dataList = res.data.user;
                            this.dataList.forEach((value, index) => {
                                this.dataList[index].gender = value.gender === 0 ? '男' : value.gender === 1 ? '女' : '未设置'
                                this.dataList[index].school = value.school[0].name
                            })
                            this.total = res.data.total;
                            this.currentPage = res.data.currentPage;
                        // });
                    });

                } else {
                    data.school = this.school
                    request({
                        url: "/user/getUserList",
                        method: "post",
                        data: data
                    }).then(res => {
                        this.dataList = [];
                        this.dataList = res.data.user;

                        this.dataList.forEach((value, index) => {
                            this.dataList[index].gender = value.gender === 1 ? '男' : value.gender === 2 ? '女' : '未设置'
                            this.dataList[index].school = value.school[0].name
                        });
                        this.total = res.data.total;
                        this.currentPage = res.data.currentPage;
                    });
                }
            },
            load(){
                this.school = this.info.school[0]._id
            this.dataSearch.school = this.school
                       this.agent = this.info.staff == undefined ? null : this.info.staff._id
            this.staffname = this.info.staff == undefined ? '' : this.info.staff.name
            this.dataSearch.page = this.currentPage;
            this.dataSearch.pageSize = this.pageSize;
            this.getDataList(this.dataSearch);
            if ((this.info.school[0].headquarter == 1) || (this.info.school[0].headquarter == 2)){
                    this.headquarter = true
                    this.getSchoolList()
            }
            }
        },
        beforeRouteEnter(to, from, next) {
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                v.load()
                })
            }
            else{
                next()
            }


        },
        beforeRouteLeave(to,from,next){
            next()
        },
        created() {
            this.load()

        }

    };
</script>
